/*
** main.c for bistromathique in /home/labonn_c//Documents/bistromathique
** 
** Made by christophe labonnemartinez
** Login   <labonn_c@epitech.net>
** 
** Started on  Mon Oct 31 10:47:37 2011 christophe labonnemartinez
** Last update Wed Nov  2 16:15:34 2011 christophe labonnemartinez
*/

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "include/bistromathique.h"
#include "lib/my.h"

int		main(int argc, char **argv)
{
  char		*expr;
  unsigned int	size;

  if (argc != 4)
    {
      my_putstr("Usage : ");
      my_putstr(argv[0]);
      my_putstr(" base ops\"()+*-/%\" exp_len\n");
      exit(1);
    }
  check_base(argv[1]);
  check_ops(argv[2]);
  expr = get_expr(size = atoi(argv[3]));
  my_putstr(eval_expr(argv[1]));
  return (0);
}

static void	check_base(char *b)
{
  if (my_strlen(b) < 2)
    {
      my_putstr("Bad base\n");
      exit(1);
    }
}

static char	*get_expr(unsigned int size)
{
  char	*expr;

  if (size <= 0)
    {
      my_putstr("Bad expr len\n");
      exit(1);
    }
  if ((expr = malloc(size + 1)) == 0)
    {
      my_putstr("could not alloc\n");
      exit(1);
    }
  if (read(0, expr, size) != size)
    {
      my_putstr("could not read\n");
      exit(1);
    }
  expr[size] = 0;
  return (expr);
}

static void	check_ops(char *ops)
{
  if (strlen(ops) != 7)
    {
      my_putstr("Bad ops\n");
      exit(1);
    }
}
